﻿package core.ui
{
    import core.enum.*;
    import core.event.*;
    import core.image.*;
    import core.ui.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    public class HistoryPanel extends Box
    {
        private var scroller:Scrollbar;
        private var history:History;
        private var bbar:Sprite;
        private var grip:Sprite;
        private var holder:Sprite;
        var marquee:BitmapData;
        var crop:BitmapData;
        var lasso:BitmapData;
        var menu:BitmapData;
        var move:BitmapData;
        var type:BitmapData;
        var clone:BitmapData;
        var brush:BitmapData;
        var pencil:BitmapData;
        var fill:BitmapData;
        var wand:BitmapData;
        var blur:BitmapData;
        var sharpen:BitmapData;
        var smudge:BitmapData;
        var burn:BitmapData;
        var dodge:BitmapData;
        var sponge:BitmapData;
        var bloat:BitmapData;
        var drawing:BitmapData;
        var pinch:BitmapData;
        var eraser:BitmapData;
        var redeye:BitmapData;
        var gradient:BitmapData;
        var colorreplace:BitmapData;
        var spotheal:BitmapData;

        public function HistoryPanel(param1:Tuyitu)
        {
            super(param1, "rm_history", 220, 177);
            var _loc_2:* = new BoxSplit("medium");
            _loc_2.x = 11;
            _loc_2.y = 28;
            addChild(_loc_2);
            this.holder = new Sprite();
            this.holder.addEventListener(MouseEvent.ROLL_OVER, this.HolderMouseOver, false, 0, true);
            this.holder.addEventListener(MouseEvent.ROLL_OUT, this.HolderMouseOut, false, 0, true);
            this.holder.x = 4;
            this.holder.y = 30;
            addChild(this.holder);
            this.scroller = new Scrollbar(this.holder, new Rectangle(0, 0, 212, 120));
            this.scroller.addEventListener(ScrollEvent.APPEARANCE, this.Update, false, 1, true);
            this.scroller.x = 200;
            this.scroller.y = 30;
            addChild(this.scroller);
            this.bbar = new Sprite();
            var _loc_3:* = new BoxSplit("medium");
            _loc_3.x = 11;
            _loc_3.y = 0;
            this.bbar.addChild(_loc_3);
            this.bbar.x = 4;
            this.bbar.y = 151;
            addChild(this.bbar);
            this.grip = new Sprite();
            this.grip.addChild(new Bitmap(new DragMark()));
            this.grip.addEventListener(MouseEvent.MOUSE_DOWN, this.GripMouseDown, false, 0, true);
            this.grip.addEventListener(MouseEvent.MOUSE_UP, this.GripMouseUp, false, 0, true);
            this.grip.addEventListener(MouseEvent.MOUSE_OVER, this.GripMouseOver, false, 0, true);
            this.grip.addEventListener(MouseEvent.MOUSE_OUT, this.GripMouseOut, false, 0, true);
            this.grip.x = 203;
            this.grip.y = 159;
            addChild(this.grip);
            return;
        }// end function

        public function SetReference(HolderMouseOver:History) : void
        {
            this.history = HolderMouseOver;
            this.Update();
            return;
        }// end function

        public function Update(... args) : void
        {
            args = 0;
            var _loc_3:HistoryEntry = null;
            var _loc_4:HistoryEntryInfo = null;
            while (this.holder.numChildren > 0)
            {
                
                this.holder.removeChildAt(0);
            }
            if (this.history != null)
            {
                args = 0;
                while (args < this.history.undo.length)
                {
                    
                    _loc_3 = this.history.undo[args] as HistoryEntry;
                    _loc_4 = new HistoryEntryInfo(this.history.undo.length - args - 1, _loc_3.type.name, this.GetIcon(_loc_3.type), false, args == (this.history.undo.length - 1), this.scroller.visible ? (196) : (212));
                    _loc_4.addEventListener(MouseEvent.MOUSE_DOWN, this.EntryMouseDown, false, 0, true);
                    _loc_4.y = int(26 * args);
                    this.holder.addChild(_loc_4);
                    args++;
                }
                args = 0;
                while (args < this.history.redo.length)
                {
                    
                    _loc_3 = this.history.redo[(this.history.redo.length - 1) - args] as HistoryEntry;
                    _loc_4 = new HistoryEntryInfo((args + 1), _loc_3.type.name, this.GetIcon(_loc_3.type), true, false, this.scroller.visible ? (196) : (212));
                    _loc_4.addEventListener(MouseEvent.MOUSE_DOWN, this.EntryMouseDown, false, 0, true);
                    _loc_4.y = int(26 * (args + this.history.undo.length));
                    this.holder.addChild(_loc_4);
                    args++;
                }
            }
            this.scroller.SetContentHeight(this.holder.numChildren * 26, Boolean(this.history != null && this.history.redo.length == 0));
            return;
        }// end function

        private function EntryMouseDown(event:MouseEvent) : void
        {
            var _loc_2:* = event.currentTarget as HistoryEntryInfo;
            if (!_loc_2.marked)
            {
                if (_loc_2.disabled)
                {
                    this.history.Redo(_loc_2.nr);
                }
                else
                {
                    this.history.Undo(_loc_2.nr);
                }
            }
            return;
        }// end function

        private function HolderMouseOver(event:MouseEvent) : void
        {
            ed.cursor.Set(ToolType.Click);
            return;
        }// end function

        private function HolderMouseOut(event:MouseEvent) : void
        {
            ed.cursor.Set(ToolType.None);
            return;
        }// end function

        private function GripMouseOver(event:MouseEvent) : void
        {
            ed.cursor.Set(ToolType.ResizeHeight);
            return;
        }// end function

        private function GripMouseOut(event:MouseEvent) : void
        {
            ed.cursor.Set(ToolType.None);
            return;
        }// end function

        private function GripMouseMove(event:MouseEvent) : void
        {
            this.Resize(220, this.grip.y + 18);
            this.scroller.SetSize(this.grip.y - 39);
            this.bbar.y = this.grip.y - 8;
            return;
        }// end function

        private function GripMouseDown(event:MouseEvent) : void
        {
            ed.cursor.freeze = true;
            this.grip.startDrag(false, new Rectangle(this.grip.x, 103, 0, 600));
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.GripMouseMove, false, 0, true);
            stage.addEventListener(MouseEvent.MOUSE_UP, this.GripMouseUp, false, 0, true);
            return;
        }// end function

        private function GripMouseUp(event:MouseEvent) : void
        {
            this.grip.stopDrag();
            ed.cursor.freeze = false;
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.GripMouseMove, false);
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.GripMouseUp, false);
            this.GripMouseMove(null);
            if (event.target != this.grip)
            {
                ed.cursor.Set(ToolType.None);
            }
            return;
        }// end function

        private function GetIcon(MarqueeIcon:Action) : BitmapData
        {
            switch(MarqueeIcon)
            {
                case Action.DrawRect:
                case Action.DrawEllipse:
                case Action.DrawRoundRect:
                case Action.DrawLine:
                {
                    if (this.drawing == null)
                    {
                        this.drawing = UI.Desaturate(new DrawingIcon());
                    }
                    return this.drawing;
                }
                case Action.FillTool:
                {
                    if (this.fill == null)
                    {
                        this.fill = UI.Desaturate(new FillIcon());
                    }
                    return this.fill;
                }
                case Action.LayerMove:
                {
                    if (this.move == null)
                    {
                        this.move = UI.Desaturate(new MoveIcon());
                    }
                    return this.move;
                }
                case Action.BrushTool:
                {
                    if (this.brush == null)
                    {
                        this.brush = UI.Desaturate(new BrushIcon());
                    }
                    return this.brush;
                }
                case Action.PencilTool:
                {
                    if (this.pencil == null)
                    {
                        this.pencil = UI.Desaturate(new PencilIcon());
                    }
                    return this.pencil;
                }
                case Action.GradientTool:
                {
                    if (this.gradient == null)
                    {
                        this.gradient = UI.Desaturate(new GradientIcon());
                    }
                    return this.gradient;
                }
                case Action.ColorReplaceTool:
                {
                    if (this.colorreplace == null)
                    {
                        this.colorreplace = UI.Desaturate(new ColorReplaceIcon());
                    }
                    return this.colorreplace;
                }
                case Action.SpotHealTool:
                {
                    if (this.spotheal == null)
                    {
                        this.spotheal = UI.Desaturate(new SpotHealIcon());
                    }
                    return this.spotheal;
                }
                case Action.CloneTool:
                {
                    if (this.clone == null)
                    {
                        this.clone = UI.Desaturate(new CloneIcon());
                    }
                    return this.clone;
                }
                case Action.SmudgeTool:
                {
                    if (this.smudge == null)
                    {
                        this.smudge = UI.Desaturate(new SmudgeIcon());
                    }
                    return this.smudge;
                }
                case Action.RedEyeReductionTool:
                {
                    if (this.redeye == null)
                    {
                        this.redeye = UI.Desaturate(new RedEyeIcon());
                    }
                    return this.redeye;
                }
                case Action.EraserTool:
                {
                    if (this.eraser == null)
                    {
                        this.eraser = UI.Desaturate(new EraserIcon());
                    }
                    return this.eraser;
                }
                case Action.BloatTool:
                {
                    if (this.bloat == null)
                    {
                        this.bloat = UI.Desaturate(new BloatIcon());
                    }
                    return this.bloat;
                }
                case Action.PinchTool:
                {
                    if (this.pinch == null)
                    {
                        this.pinch = UI.Desaturate(new PinchIcon());
                    }
                    return this.pinch;
                }
                case Action.DodgeTool:
                {
                    if (this.dodge == null)
                    {
                        this.dodge = UI.Desaturate(new DodgeIcon());
                    }
                    return this.dodge;
                }
                case Action.BurnTool:
                {
                    if (this.burn == null)
                    {
                        this.burn = UI.Desaturate(new BurnIcon());
                    }
                    return this.burn;
                }
                case Action.SpongeTool:
                {
                    if (this.sponge == null)
                    {
                        this.sponge = UI.Desaturate(new SpongeIcon());
                    }
                    return this.sponge;
                }
                case Action.BlurTool:
                {
                    if (this.blur == null)
                    {
                        this.blur = UI.Desaturate(new BlurIcon());
                    }
                    return this.blur;
                }
                case Action.SharpenTool:
                {
                    if (this.sharpen == null)
                    {
                        this.sharpen = UI.Desaturate(new SharpenIcon());
                    }
                    return this.sharpen;
                }
                case Action.CropTool:
                {
                    if (this.crop == null)
                    {
                        this.crop = UI.Desaturate(new CropIcon());
                    }
                    return this.crop;
                }
                case Action.WandTool:
                {
                    if (this.wand == null)
                    {
                        this.wand = UI.Desaturate(new WandIcon());
                    }
                    return this.wand;
                }
                case Action.PolygonLassoTool:
                case Action.FreehandLassoTool:
                {
                    if (this.lasso == null)
                    {
                        this.lasso = UI.Desaturate(new LassoIcon());
                    }
                    return this.lasso;
                }
                case Action.EllipticalMarqueeTool:
                case Action.RectangularMarqueeTool:
                {
                    if (this.marquee == null)
                    {
                        this.marquee = UI.Desaturate(new MarqueeIcon());
                    }
                    return this.marquee;
                }
                case Action.TypeTool:
                case Action.LayerAddText:
                {
                    if (this.type == null)
                    {
                        this.type = UI.Desaturate(new TypeIcon());
                    }
                    return this.type;
                }
                default:
                {
                    if (this.menu == null)
                    {
                        this.menu = new MenuIcon();
                    }
                    return this.menu;
                    break;
                }
            }
        }// end function

    }
}
